/**
 * @version		$Id: listsorter.js 36 2011-01-20 12:59:29Z happy_noodle_boy $
 * @package      JCE
 * @copyright    Copyright (C) 2005 - 2009 Ryan Demmer. All rights reserved.
 * @author		Ryan Demmer
 * @license      GNU/GPL
 * JCE is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */
(function($) {
    $.widget("ui.listsort", {

        options : {
            selector 	: '*',
            type     	: 'name',
            lists		: []
        },

        _init : function() {
            var self = this;

            $(this.element).addClass('asc').click( function(e) {
                self._sortList();
            });

        },

        _sortList : function() {
            var self = this, dir;

            dir = $(this.element).hasClass('asc') ? 'desc' : 'asc';

            $(this.element).attr('class', dir);

            $.each(this.options.lists, function(i, n) {
                self._sortItems(n, dir, self.options.type);
            });

        },

        _sortItems : function(n, dir, type) {
            var a = this._getSortCache(n, type);

            switch (type) {
                case 'date' :
                    fn = this._sortDate;
                    break;
                default:
                    fn = this._sortCompare;
                    break;
            }

            a.sort(fn);

            if (dir == 'desc' || type == 'ext')
                a.reverse();

            // remove from doc
            $(this.options.selector, $(n)).remove();

            // insert in the new order
            $.each(a, function(i, o) {
                $(n).append(o.element);
            });

            a = null;

            this._trigger('onSort');
        },

        _sortDate : function(a, b) {
            var x = a.value, y = b.value, r = 0, d1 = 0, d2 = 0, t1 = 0, t2 = 0;
            var re = /(\d{2})[\/](\d{2})[\/](\d{4}), (\d{2})[:](\d{2})/g;

            d1 = x.replace(re, '$3$2$1');
            d2 = y.replace(re, '$3$2$1');

            t1 = x.replace(re, '$4$5');
            t2 = y.replace(re, '$4$5');

            // sort date
            if (d1 > d2) {
                r = 1;
            }

            if (d1 < d2) {
                r = -1;
            }

            // sort time
            if (t1 > t2) {
                r = 1;
            }

            if (t1 < t2) {
                r = -1;
            }

            return r;
        },

        _sortCompare : function(a, b) {
            if (a.value < b.value) {
                return -1;
            }
            if (b.value < a.value) {
                return 1;
            }
            return 0;
        },

        _getSortCache : function(n, type) {
            var a = [], v, x = 0;

            $(this.options.selector, n).each( function() {
                v = $(this).attr('data-' + type) || $(this).attr('title').toLowerCase();

                switch (type) {
                    case 'date':
                        break;
                    case 'ext':
                        v = v.substring(v.length, v.lastIndexOf('.') + 1).toLowerCase();
                        break;
                    case 'name':
                        break;
                    case 'size':
                        v = parseFloat(v);
                        break;
                }

                a[x] = {
                    value:   v,
                    element: this
                };
                x++;
            });

            return a;
        },

        destroy: function() {
            $.Widget.prototype.destroy.apply( this, arguments );
        }

    });

    $.extend($.ui.listsort, {
        version: "2.0.0beta2"
    });

})(jQuery);